﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace GTA.UserControls
{
    public partial class UcEMailText : BaseUserControl
    {
        #region Properties
        public override String Text
        {
            get { return txtEMail.Text; }
            set { txtEMail.Text = value; }
        }
        [Browsable(true),
        Description("Set the MaxLength of the control"),
        Category("Behavior"),
        DefaultValue(32767)]
        public Int32 MaxLength
        {
            get { return txtEMail.MaxLength; }
            set { txtEMail.MaxLength = value; }
        }
        #endregion

        #region Start Control
        public UcEMailText()
        {
            InitializeComponent();
        }
        #endregion

        #region Events
        private void txtEMail_Leave(object sender, EventArgs e)
        {
            ValidateEMailFormat();
        }
        #endregion

        #region Methods
        private void ValidateEMailFormat()
        {
            if (!String.IsNullOrEmpty(txtEMail.Text))
            {
                Regex rgx = new Regex("^[a-zA-Z0-9_]{1}[a-zA-Z0-9_\\.\\-]*[a-zA-Z0-9_]{1}@[a-zA-Z0-9_]{1}[a-zA-Z0-9_\\.\\-]*[a-zA-Z0-9_]{1}.([a-zA-Z]{2,3}){1,2}$");
                if (!rgx.IsMatch(txtEMail.Text))
                {
                    MessageBox.Show("E-Mail em formato inválido!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txtEMail.Focus();
                    txtEMail.SelectAll();
                }
            }
        }
        #endregion
    }
}
